package com.example.demo.contorller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;

@RestController
public class UserContorller {
	
	@Autowired
	private UserService	userService;
	
	@RequestMapping("/getUser/{userName}")
	public User getUser(@PathVariable String userName) {
		User user = userService.selectUserByName(userName);
		if(user != null) {
			return user;
		}else {
			return null;
		}
	}
	
	@GetMapping("/getString")
	public String print() {
		return "Hello Server_DB";
	}
	
	@GetMapping("/getUserById/{uid}")
	public User getUser2(@PathVariable int uid) {
		return userService.selectById(uid);	
	}
	
	@GetMapping("/deleteUserId/{uid}")
	public int deleteUserById(@PathVariable int uid) {
	
		return  userService.deleteUser(uid);
	}
	@GetMapping("/getAllUser")
	public List<User> getAllUser(){
		List<User> list = userService.selectAllUser();
		return list;
	}
	
	@PostMapping("/insertUser")
	public int insertUser(@RequestBody User user){
		return userService.insertUser(user);
		
	}
	@PostMapping("updateUser")
	public int updateUser(@RequestBody User user){
		return  userService.updateUser(user);
		
	}
	
	@RequestMapping("/addUser")
	public int addUser() {
		List<User> list = new ArrayList<User>();
		list.add(new User("py2","py"));
		list.add(new User("sty2","sty"));
		list.add(new User("wss2","wss"));
		list.add(new User("mss2","mss"));
		
		list.add(new User("rnn2","rnn"));
		list.add(new User("wss12","wss1"));
		list.add(new User("lyc2","lyc"));
		
		list.add(new User("zyt2","zyt"));
		list.add(new User("ccm2","ccm"));
		list.add(new User("lwm2","lwm"));
		
		
		return userService.addUser2(list);
	}
	
	//批量更新有问题
	@GetMapping("/updateUserTwo/{flag}")
	public int updateUserTwo(@PathVariable int flag) {
		List<User> list = new ArrayList<User>();
		list.add(new User("zyy","1997",flag));
		
		return userService.updateUserTwo(list);
	}
}
